package fast.mvc;

import java.awt.Component;
import java.awt.Image;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingWorker;

class ChangeIconTask extends SwingWorker<Image,Object> {
	
	private Image image;
	private Component icon = null;
	private String name;
	private boolean scaleDown = false;
	private boolean noScale = false;

	public ChangeIconTask(Component icon,String name) {
		this.icon = icon;
		this.name = name.toLowerCase();
	}
	
	public ChangeIconTask(Component icon,String name,boolean scaleDown) {
		this(icon,name);
		this.scaleDown = scaleDown;
	}
	
	public ChangeIconTask(Component icon,String name,boolean scaleDown,boolean noScale) {
		this(icon,name,scaleDown);
		this.noScale = noScale;
	}
	
	@Override
	protected Image doInBackground() throws Exception {
		URL url = null, url2 = null;
		File iconDir = new File("./icons/");
		if(!iconDir.exists())
			iconDir.mkdir();
		File file = new File("./icons/"+name+".jpg");
		if (!file.exists()) {
			try {
				url2 = new URL("http://us.media.blizzard.com/wow/icons/56/"+name+".jpg");
				url2 = new URL("https://wowimg.zamimg.com/images/wow/icons/large/"+name.toLowerCase()+".jpg");
				url = url2;
				image = ImageIO.read(url);
				if(!noScale)
					image = icon.createImage(new FilteredImageSource(image.getSource(),
							new CropImageFilter(1, 1, 54, 54)));
				if(scaleDown)
					image = image.getScaledInstance(14, 14, Image.SCALE_DEFAULT);
				ReadableByteChannel rbc = Channels.newChannel(url2.openStream());
				FileOutputStream fos = new FileOutputStream(new File("./icons/"+name+".jpg"));
				fos.getChannel().transferFrom(rbc, 0, 1 << 24);
				fos.close();
			}
			catch (MalformedURLException e) {e.printStackTrace();}
			catch (Exception e) {
				System.out.println("icon not found: " + name);
				View.showError("icon not found: " + name);
				try {
					image = ImageIO.read(new File("./icons/inv_misc_questionmark.jpg"));
					if(!noScale)
						image = icon.createImage(new FilteredImageSource(image.getSource(),
								new CropImageFilter(1, 1, 54, 54)));
					if(scaleDown)
						image = image.getScaledInstance(14, 14, Image.SCALE_DEFAULT);
				} catch (IOException ee) {ee.printStackTrace();}
			}
		} else {
			try {
				image = ImageIO.read(file);
				if(!noScale)
					image = icon.createImage(new FilteredImageSource(image.getSource(),
							new CropImageFilter(1, 1, 54, 54)));
				if(scaleDown)
					image = image.getScaledInstance(14, 14, Image.SCALE_DEFAULT);
			} catch (IOException e) {e.printStackTrace();}

		}
		return image;
	}
	
	protected Image doInBackground2() throws Exception {
		URL url = null, url2 = null;
		File file = new File("./icons/"+name+".png");
		if (!file.exists()) {
			//System.out.println("load from mmo-champ");
			try {
				url2 = new URL("http://db.mmo-champion.com/static/img/icons/"+name+".png");
				url = url2;
				image = ImageIO.read(url);
				if(!noScale)
					image = icon.createImage(new FilteredImageSource(image.getSource(),
							new CropImageFilter(5, 5, 54, 54)));
				if(scaleDown)
					image = image.getScaledInstance(14, 14, Image.SCALE_DEFAULT);
				ReadableByteChannel rbc = Channels.newChannel(url2.openStream());
				FileOutputStream fos = new FileOutputStream(new File("./icons/"+name+".png"));
				fos.getChannel().transferFrom(rbc, 0, 1 << 24);
				fos.close();
			}
			catch (MalformedURLException e) {
				e.printStackTrace();
			}
			catch (Exception e) {
				System.out.println("icon not found: " + name);
				View.showError("icon not found: " + name);
				try {
					image = ImageIO.read(new File("./icons/inv_misc_questionmark.png"));
					if(!noScale)
						image = icon.createImage(new FilteredImageSource(image.getSource(),
								new CropImageFilter(5, 5, 54, 54)));
					if(scaleDown)
						image = image.getScaledInstance(14, 14, Image.SCALE_DEFAULT);
				} catch (IOException ee) {
					ee.printStackTrace();
				}
			}
		} else {
			//System.out.println("load from icon folder");
			try {
				image = ImageIO.read(file);
				if(!noScale)
					image = icon.createImage(new FilteredImageSource(image.getSource(),
							new CropImageFilter(5, 5, 54, 54)));
				if(scaleDown)
					image = image.getScaledInstance(14, 14, Image.SCALE_DEFAULT);
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
		return image;
	}
	
	@Override 
	protected void done()
    {
		try
		{
			if(icon instanceof JLabel)
				((JLabel) icon).setIcon(new ImageIcon(get()));
			else if(icon instanceof JButton)
				((JButton) icon).setIcon(new ImageIcon(get()));
		}
		catch ( /* InterruptedException, ExecutionException */ Exception e ) { 
			e.printStackTrace();
		}
    }
}
